package com.ctbri.concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Runnable1 implements Runnable {
	@Override
	public synchronized void run() {
		try {
			wait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("run1 finished.");
	}
}

class Runnable2 implements Runnable {
	private Runnable1 run1;
	public Runnable2(Runnable1 run1) {
		this.run1 = run1;
	}
	@Override
	public void run() {
		try {
			TimeUnit.SECONDS.sleep(1);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		synchronized(run1) {
			run1.notifyAll();
		}
	}
}
public class Ex21 {

	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		Runnable1 run1 = new Runnable1();
		exec.execute(run1);
		exec.execute(new Runnable2(run1));
		exec.shutdown();
	}

}
